1 // ----------------------------------------------------------------------------
2 // <copyright file=
"PhotonViewInspector.cs" company="Exit Games GmbH">
3 // PhotonNetwork Framework
for Unity - Copyright (C) 2011 Exit Games GmbH
4 // </copyright>
5 // <summary>
6 // Custom inspector
for the PhotonView component.
7 // </summary>
8 // <author>developer@exitgames.com</author>
9 // ----------------------------------------------------------------------------

10
11 using
System;
12 using
UnityEditor;
13 using
UnityEngine;
14 using
Rotorz.ReorderableList.Internal;
15
16 [CustomEditor(
typeof(PhotonView))]
17 public
class PhotonViewInspector : Editor
18 {
19     PhotonView m_Target;
20
21     
public override void OnInspectorGUI()
22     {
23         #
if UNITY_3_5
24         EditorGUIUtility.LookLikeInspector();
25         #endif
26         
//EditorGUI.indentLevel = 1;
27
28         m_Target = (PhotonView)
this.target;
29         
bool isProjectPrefab = EditorUtility.IsPersistent(m_Target.gameObject);
30
31         
if( m_Target.ObservedComponents == null )
32         {
33             m_Target.ObservedComponents =
new System.Collections.Generic.List<Component>();
34         }
35
36         
if( m_Target.ObservedComponents.Count == 0 )
37         {
38             m_Target.ObservedComponents.Add(
null );
39         }
40
41         EditorGUILayout.BeginHorizontal();
42         
// Owner
43         
if (isProjectPrefab)
44         {
45             EditorGUILayout.LabelField(
"Owner:", "Set at runtime");
46         }
47         
else if (m_Target.isSceneView)
48         {
49             EditorGUILayout.LabelField(
"Owner", "Scene");
50         }
51         
else
52         {
53             PhotonPlayer owner = m_Target.owner;
54             
string ownerInfo = (owner != null) ? owner.name : "<no PhotonPlayer found>";
55
56             
if (string.IsNullOrEmpty(ownerInfo))
57             {
58                 ownerInfo =
"<no playername set>";
59             }
60
61             EditorGUILayout.LabelField(
"Owner", "[" + m_Target.ownerId + "] " + ownerInfo);
62         }
63
64         
// ownership requests
65         EditorGUI.BeginDisabledGroup(Application.isPlaying);
66         m_Target.ownershipTransfer = (OwnershipOption)EditorGUILayout.EnumPopup(m_Target.ownershipTransfer, GUILayout.Width(
100));
67         EditorGUI.EndDisabledGroup();
68
69         EditorGUILayout.EndHorizontal();
70
71
72         
// View ID
73         
if (isProjectPrefab)
74         {
75             EditorGUILayout.LabelField(
"View ID", "Set at runtime");
76         }
77         
else if (EditorApplication.isPlaying)
78         {
79             EditorGUILayout.LabelField(
"View ID", m_Target.viewID.ToString());
80         }
81         
else
82         {
83             
int idValue = EditorGUILayout.IntField("View ID [1.."+(PhotonNetwork.MAX_VIEW_IDS-1)+"]", m_Target.viewID);
84             m_Target.viewID = idValue;
85         }
86
87
88
89         
// Locally Controlled
90         
if (EditorApplication.isPlaying)
91         {
92             
string masterClientHint = PhotonNetwork.isMasterClient ? "(master)" : "";
93             EditorGUILayout.Toggle(
"Controlled locally: " + masterClientHint, m_Target.isMine);
94         }
95
96
97
98         
//DrawOldObservedItem();
99         ConvertOldObservedItemToObservedList();
100
101
102         
// ViewSynchronization (reliability)
103         
if (m_Target.synchronization == ViewSynchronization.Off)
104         {
105             GUI.color = Color.grey;
106         }
107
108         EditorGUILayout.PropertyField( serializedObject.FindProperty(
"synchronization" ), new GUIContent( "Observe option:" ) );
109
110         
if( m_Target.synchronization != ViewSynchronization.Off &&
111             m_Target.ObservedComponents.FindAll( item => item !=
null ).Count == 0 )
112         {
113             GUILayout.BeginVertical( GUI.skin.box );
114             GUILayout.Label(
"Warning", EditorStyles.boldLabel );
115             GUILayout.Label(
"Setting the synchronization option only makes sense if you observe something." );
116             GUILayout.EndVertical();
117         }
118
119         
/*ViewSynchronization vsValue = (ViewSynchronization)EditorGUILayout.EnumPopup("Observe option:", m_Target.synchronization);
120         
if (vsValue != m_Target.synchronization)
121         {
122             m_Target.synchronization = vsValue;
123             
if (m_Target.synchronization != ViewSynchronization.Off && m_Target.observed == null)
124             {
125                 EditorUtility.DisplayDialog(
"Warning", "Setting the synchronization option only makes sense if you observe something.", "OK, I will fix it.");
126             }
127         }*/

128
129         DrawSpecificTypeSerializationOptions();
130
131         GUI.color = Color.white;
132         DrawObservedComponentsList();
133
134         
// Cleanup: save and fix look
135         
if (GUI.changed)
136         {
137             EditorUtility.SetDirty(m_Target);
138             PhotonViewHandler.HierarchyChange();
// TODO: check if needed
139         }
140
141         GUI.color = Color.white;
142         EditorGUIUtility.LookLikeControls();
143     }
144
145     
void DrawSpecificTypeSerializationOptions()
146     {
147         
if( m_Target.ObservedComponents.FindAll( item => item != null && item.GetType() == typeof( Transform ) ).Count > 0 ||
148             ( m_Target.observed !=
null && m_Target.observed.GetType() == typeof( Transform ) ) )
149         {
150             m_Target.onSerializeTransformOption = (OnSerializeTransform)EditorGUILayout.EnumPopup(
"Transform Serialization:", m_Target.onSerializeTransformOption );
151
152         }
153         
else if( m_Target.ObservedComponents.FindAll( item => item != null && item.GetType() == typeof( Rigidbody ) ).Count > 0 ||
154             ( m_Target.observed !=
null && m_Target.observed.GetType() == typeof( Rigidbody ) ) ||
155             m_Target.ObservedComponents.FindAll( item => item !=
null && item.GetType() == typeof( Rigidbody2D ) ).Count > 0 ||
156             ( m_Target.observed !=
null && m_Target.observed.GetType() == typeof( Rigidbody2D ) ) )
157         {
158             m_Target.onSerializeRigidBodyOption = (OnSerializeRigidBody)EditorGUILayout.EnumPopup(
"Rigidbody Serialization:", m_Target.onSerializeRigidBodyOption );
159         }
160     }
161
162     
void DrawSpecificTypeOptions()
163     {
164         
if( m_Target.observed != null )
165         {
166             Type type = m_Target.observed.GetType();
167             
if( type == typeof( Transform ) )
168             {
169                 m_Target.onSerializeTransformOption = (OnSerializeTransform)EditorGUILayout.EnumPopup(
"Serialization:", m_Target.onSerializeTransformOption );
170
171             }
172             
else if( type == typeof( Rigidbody ) )
173             {
174                 m_Target.onSerializeRigidBodyOption = (OnSerializeRigidBody)EditorGUILayout.EnumPopup(
"Serialization:", m_Target.onSerializeRigidBodyOption );
175
176             }
177         }
178     }
179
180     
void ConvertOldObservedItemToObservedList()
181     {
182         
if( m_Target.observed != null )
183         {
184             
if( m_Target.ObservedComponents.Contains( m_Target.observed ) == false )
185             {
186                 
bool wasAdded = false;
187
188                 
for( int i = 0; i < m_Target.ObservedComponents.Count; ++i )
189                 {
190                     
if( m_Target.ObservedComponents[ i ] == null )
191                     {
192                         m_Target.ObservedComponents[ i ] = m_Target.observed;
193                         wasAdded =
true;
194                     }
195                 }
196
197                 
if( wasAdded == false )
198                 {
199                     m_Target.ObservedComponents.Add( m_Target.observed );
200                 }
201             }
202
203             m_Target.observed =
null;
204             EditorUtility.SetDirty( m_Target );
205         }
206     }
207
208
209     
void DrawOldObservedItem()
210     {
211         EditorGUILayout.BeginHorizontal();
212
213         
// Using a lower version then 3.4? Remove the TRUE in the next line to fix an compile error
214         
string typeOfObserved = string.Empty;
215         
if( m_Target.observed != null )
216         {
217             
int firstBracketPos = m_Target.observed.ToString().LastIndexOf( '(' );
218             
if( firstBracketPos > 0 )
219             {
220                 typeOfObserved = m_Target.observed.ToString().Substring( firstBracketPos );
221             }
222         }
223
224
225         Component componenValue = (Component)EditorGUILayout.ObjectField(
"Observe: " + typeOfObserved, m_Target.observed, typeof( Component ), true );
226         
if( m_Target.observed != componenValue )
227         {
228             
if( m_Target.observed == null )
229             {
230                 m_Target.synchronization = ViewSynchronization.UnreliableOnChange;
// if we didn't observe anything yet. use unreliable on change as default
231             }
232             
if( componenValue == null )
233             {
234                 m_Target.synchronization = ViewSynchronization.Off;
235             }
236
237             m_Target.observed = componenValue;
238         }
239
240         EditorGUILayout.EndHorizontal();
241     }
242
243     
int GetObservedComponentsCount()
244     {
245         
int count = 0;
246
247         
for( int i = 0; i < m_Target.ObservedComponents.Count; ++i )
248         {
249             
if( m_Target.ObservedComponents[ i ] != null )
250             {
251                 count++;
252             }
253         }
254
255         
return count;
256     }
257
258     
void DrawObservedComponentsList()
259     {
260         GUILayout.Space(
5 );
261         SerializedProperty listProperty = serializedObject.FindProperty(
"ObservedComponents" );
262
263         
if( listProperty == null )
264         {
265             
return;
266         }
267
268         
float containerElementHeight = 22;
269         
float containerHeight = listProperty.arraySize * containerElementHeight;
270
271         
bool isOpen = PhotonGUI.ContainerHeaderFoldout( "Observed Components (" + GetObservedComponentsCount() + ")", serializedObject.FindProperty( "ObservedComponentsFoldoutOpen" ).boolValue );
272         serializedObject.FindProperty(
"ObservedComponentsFoldoutOpen" ).boolValue = isOpen;
273
274         
if( isOpen == false )
275         {
276             containerHeight =
0;
277         }
278
279         
//Texture2D statsIcon = AssetDatabase.LoadAssetAtPath( "Assets/Photon Unity Networking/Editor/PhotonNetwork/PhotonViewStats.png", typeof( Texture2D ) ) as Texture2D;
280
281         Rect containerRect = PhotonGUI.ContainerBody( containerHeight );
282         
bool wasObservedComponentsEmpty = m_Target.ObservedComponents.FindAll( item => item != null ).Count == 0;
283         
if( isOpen == true )
284         {
285             
for( int i = 0; i < listProperty.arraySize; ++i )
286             {
287                 Rect elementRect =
new Rect( containerRect.xMin, containerRect.yMin + containerElementHeight * i, containerRect.width, containerElementHeight );
288                 {
289                     Rect texturePosition =
new Rect( elementRect.xMin + 6, elementRect.yMin + elementRect.height / 2f - 1, 9, 5 );
290                     ReorderableListResources.DrawTexture( texturePosition, ReorderableListResources.texGrabHandle );
291
292                     Rect propertyPosition =
new Rect( elementRect.xMin + 20, elementRect.yMin + 3, elementRect.width - 45, 16 );
293                     EditorGUI.PropertyField( propertyPosition, listProperty.GetArrayElementAtIndex( i ),
new GUIContent() );
294
295                     
//Debug.Log( listProperty.GetArrayElementAtIndex( i ).objectReferenceValue.GetType() );
296                     
//Rect statsPosition = new Rect( propertyPosition.xMax + 7, propertyPosition.yMin, statsIcon.width, statsIcon.height );
297                     
//ReorderableListResources.DrawTexture( statsPosition, statsIcon );
298
299                     Rect removeButtonRect =
new Rect( elementRect.xMax - PhotonGUI.DefaultRemoveButtonStyle.fixedWidth,
300                                                         elementRect.yMin +
2,
301                                                         PhotonGUI.DefaultRemoveButtonStyle.fixedWidth,
302                                                         PhotonGUI.DefaultRemoveButtonStyle.fixedHeight );
303
304                     GUI.enabled = listProperty.arraySize >
1;
305                     
if( GUI.Button( removeButtonRect, new GUIContent( ReorderableListResources.texRemoveButton ), PhotonGUI.DefaultRemoveButtonStyle ) )
306                     {
307                         listProperty.DeleteArrayElementAtIndex( i );
308                     }
309                     GUI.enabled =
true;
310
311                     
if( i < listProperty.arraySize - 1 )
312                     {
313                         texturePosition =
new Rect( elementRect.xMin + 2, elementRect.yMax, elementRect.width - 4, 1 );
314                         PhotonGUI.DrawSplitter( texturePosition );
315                     }
316                 }
317             }
318         }
319
320         
if( PhotonGUI.AddButton() )
321         {
322             listProperty.InsertArrayElementAtIndex( Mathf.Max(
0, listProperty.arraySize - 1 ) );
323         }
324
325         serializedObject.ApplyModifiedProperties();
326
327         
bool isObservedComponentsEmpty = m_Target.ObservedComponents.FindAll( item => item != null ).Count == 0;
328
329         
if( wasObservedComponentsEmpty == true && isObservedComponentsEmpty == false && m_Target.synchronization == ViewSynchronization.Off )
330         {
331             m_Target.synchronization = ViewSynchronization.UnreliableOnChange;
332             EditorUtility.SetDirty( m_Target );
333             serializedObject.Update();
334         }
335
336         
if( wasObservedComponentsEmpty == false && isObservedComponentsEmpty == true )
337         {
338             m_Target.synchronization = ViewSynchronization.Off;
339             EditorUtility.SetDirty( m_Target );
340             serializedObject.Update();
341         }
342
343     }
344
345     
private static GameObject GetPrefabParent(GameObject mp)
346     {
347         #
if UNITY_2_6_1 || UNITY_2_6 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4
348         
// Unity 3.4 and older use EditorUtility
349         
return (EditorUtility.GetPrefabParent(mp) as GameObject);
350         #
else
351         
// Unity 3.5 uses PrefabUtility
352         
return PrefabUtility.GetPrefabParent(mp) as GameObject;
353         #endif
354     }
355 }


----------------------------------------------------------------------------

PhotonNetwork Framework for Unity - Copyright (C) 2011 Exit Games GmbH

Custom inspector for the PhotonView component.

developer@exitgames.com

----------------------------------------------------------------------------

EditorGUI.indentLevel = 1;

Owner

ownership requests

View ID

Locally Controlled

DrawOldObservedItem();

ViewSynchronization (reliability)

Cleanup: save and fix look

PhotonViewHandler.HierarchyChange(); TODO: check if needed

Using a lower version then 3.4? Remove the TRUE in the next line to fix an compile error

m_Target.synchronization = ViewSynchronization.UnreliableOnChange; if we didn't observe anything yet. use unreliable on change as default

Texture2D statsIcon = AssetDatabase.LoadAssetAtPath( "AssetsPhoton Unity NetworkingEditorPhotonNetworkPhotonViewStats.png", typeof( Texture2D ) ) as Texture2D;

Debug.Log( listProperty.GetArrayElementAtIndex( i ).objectReferenceValue.GetType() );

Rect statsPosition = new Rect( propertyPosition.xMax + 7, propertyPosition.yMin, statsIcon.width, statsIcon.height );

ReorderableListResources.DrawTexture( statsPosition, statsIcon );

Unity 3.4 and older use EditorUtility

Unity 3.5 uses PrefabUtility




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.556 lượt xem

Gõ tìm kiếm nhanh...